// AJAX通信函数
function ajax(options) {
    options = options || {};
    options.type = (options.type || "GET").toUpperCase();
    options.dataType = options.dataType || "json";
    
    var params = formatParams(options.data);
    
    var xhr = new XMLHttpRequest();
    
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            var status = xhr.status;
            if (status >= 200 && status < 300) {
                options.success && options.success(xhr.responseText, xhr.responseXML);
            } else {
                options.error && options.error(status);
            }
        }
    }
    
    if (options.type == "GET") {
        xhr.open("GET", options.url + "?" + params, true);
        xhr.send(null);
    } else if (options.type == "POST") {
        xhr.open("POST", options.url, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(params);
    }
}

// 格式化参数
function formatParams(data) {
    var arr = [];
    for (var name in data) {
        arr.push(encodeURIComponent(name) + "=" + encodeURIComponent(data[name]));
    }
    arr.push(("v=" + Math.random()).replace(".",""));
    return arr.join("&");
}

// 显示遮罩层
function showMask(content, buttons) {
    var mask = document.createElement("div");
    mask.className = "mask";
    
    var box = document.createElement("div");
    box.className = "mask-box";
    
    // 关闭按钮
    var close = document.createElement("div");
    close.className = "mask-close";
    close.innerHTML = "×";
    close.onclick = function() {
        document.body.removeChild(mask);
    }
    
    // 内容区域
    var contentDiv = document.createElement("div");
    contentDiv.className = "mask-content";
    contentDiv.innerHTML = content;
    
    // 按钮区域
    var btnDiv = document.createElement("div");
    btnDiv.className = "mask-buttons";
    
    if(buttons && buttons.length) {
        buttons.forEach(function(btn) {
            var button = document.createElement("button");
            button.innerHTML = btn.text;
            button.onclick = btn.click;
            btnDiv.appendChild(button);
        });
    }
    
    box.appendChild(close);
    box.appendChild(contentDiv);
    box.appendChild(btnDiv);
    mask.appendChild(box);
    document.body.appendChild(mask);
}

// Cookie操作
function setCookie(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {   
    document.cookie = name+'=; Max-Age=-99999999;';  
} 